package main

func productExceptSelf(nums []int) []int {
	numsLen := len(nums)
	leftNums := make([]int, numsLen)
	rightNums := make([]int, numsLen)
	result := make([]int, numsLen)
	for i := 0; i < numsLen; i++ {
		if i == 0 {
			leftNums[i] = nums[i]
		} else {
			leftNums[i] = nums[i] * leftNums[i-1]
		}
	}
	for i := numsLen - 1; i >= 0; i-- {
		if i == numsLen-1 {
			rightNums[i] = nums[i]
		} else {
			rightNums[i] = nums[i] * rightNums[i+1]
		}
	}
	for i := 0; i < numsLen; i++ {
		if i == 0 {
			result[i] = rightNums[i+1]
		} else if i == numsLen-1 {
			result[i] = leftNums[i-1]
		} else {
			result[i] = leftNums[i-1] * rightNums[i+1]
		}
	}
	return result
}
